#include <trace_log.h>
#include <psim_lib.h>
#include <qemu-common.h>
#include <sysemu.h>

//Trace Log file
static FILE *trace_file;

static int num_events;

typedef struct TraceEvent TraceEvent;

struct TraceEvent
{
  char name[64];
  int id;
  int counter;
};

static TraceEvent* events;

void trace_init()
{
  trace_file = fopen("sim_trace","w");
  num_events = 0;
}

int trace_register_event(char* _name)
{
  int i=num_events;
  events = realloc(events, sizeof(TraceEvent)*(++num_events));
  events[i].id = i;
  events[i].counter = 0;
  pstrcpy(events[i].name, sizeof(64), _name);

  //now write the event to log file
  //Type is Event:Event_name:Event_ID
  fprintf(trace_file, "EVENT:%s:%d\n", _name, i);

  return i;
}

inline void trace_log_event(int _id)
{
  if(is_sim_enable()) {
    //write the event to the log file
    //Format is Time:Event_ID
    fprintf(trace_file, "%u:%d\n", sim_get_clock(), _id);
    events[_id].counter++;
  }
}
